
package com.jf.cloud.multishop.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.jf.cloud.common.cache.constant.CacheNames;
import com.jf.cloud.common.cache.util.RedisUtil;
import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.util.PageUtil;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.multishop.constant.HomeStatus;
import com.jf.cloud.multishop.mapper.ShopRenovationMapper;
import com.jf.cloud.multishop.model.ShopRenovation;
import com.jf.cloud.multishop.service.ShopRenovationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 店铺装修信息
 *
 * @author zz
 * @date 2022-08-12 11:27:18
 */
@Service
public class ShopRenovationServiceImpl implements ShopRenovationService {

    @Autowired
    private ShopRenovationMapper shopRenovationMapper;

    @Override
    public PageVO<ShopRenovation> page(PageDTO pageDTO, ShopRenovation shopRenovation) {
        return PageUtil.doPage(pageDTO, () -> shopRenovationMapper.list(shopRenovation));
    }

    @Override
    @Cacheable(cacheNames = CacheNames.GET_BY_SHOP_RENOVATION_ID_KEY, key = "#renovationId")
    public ShopRenovation getByRenovationId(Long renovationId) {
        return shopRenovationMapper.getByRenovationId(renovationId);
    }

    @Override
    @Cacheable(cacheNames = CacheNames.GET_SHOP_HOME_RENOVATION_KEY, key = "#shopId + ':' + #renovationType")
    public ShopRenovation getShopHomeRenovation(Long shopId, Integer renovationType) {
        return shopRenovationMapper.getByShopIdAndRenovationType(shopId, renovationType);
    }

    @Override
    public void save(ShopRenovation shopRenovation) {
        shopRenovationMapper.save(shopRenovation);
    }

    @Override
    public void update(ShopRenovation shopRenovation) {
        shopRenovationMapper.update(shopRenovation);
    }

    @Override
    public void deleteById(Long renovationId) {
        shopRenovationMapper.deleteById(renovationId);
    }

    @Override
    public void removeCache(Long shopId, Integer renovationType, Long renovationId) {
        List<String> key = new ArrayList<>();
        key.add(CacheNames.GET_SHOP_HOME_RENOVATION_KEY + CacheNames.UNION + shopId + CacheNames.UNION_KEY + renovationType);
        key.add(CacheNames.GET_BY_SHOP_RENOVATION_ID_KEY + CacheNames.UNION + renovationId);
        RedisUtil.del(key);
    }

    @Override
    public void updateToHomePage(ShopRenovation shopRenovation) {
        // 取消设置为主页
        if (Objects.equals(shopRenovation.getHomeStatus(), HomeStatus.YES.value())) {
            shopRenovation.setHomeStatus(HomeStatus.NO.value());
            update(shopRenovation);
            return;
        }

        ShopRenovation search = new ShopRenovation();
        search.setShopId(shopRenovation.getShopId());
        search.setRenovationType(shopRenovation.getRenovationType());
        search.setHomeStatus(HomeStatus.YES.value());
        // 设为主页的装修列表
        List<ShopRenovation> shopRenovationList = shopRenovationMapper.list(search);
        // 先取消已有的主页
        shopRenovationMapper.updateHomeStatus(shopRenovation.getShopId(), shopRenovation.getRenovationType());
        // 设置当前模板为主页
        shopRenovation.setHomeStatus(HomeStatus.YES.value());
        update(shopRenovation);
        // 清除旧的主页缓存
        if (CollUtil.isNotEmpty(shopRenovationList)) {
            List<String> key = new ArrayList<>();
            for (ShopRenovation renovation : shopRenovationList) {
                key.add(CacheNames.GET_BY_SHOP_RENOVATION_ID_KEY + CacheNames.UNION + renovation.getRenovationId());
            }
            RedisUtil.del(key);
        }
    }
}
